/*
 * common.h
 *
 *  Created on: Aug 12, 2016
 *      Author: ubuntu
 */

#ifndef COMMON_H_
#define COMMON_H_

#include <iostream>
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

#include <syslog.h>
#include <errno.h>
#include <netinet/in.h>
#include "private.h"

struct custime{
	int year;
	int month;
	int day;
	int hour;
	int min;
	int sec;
};

#define HASH_SIZE	200

void set_logfile(const char *name);

void set_debuglevel(int level);

void update_logfile();

void _debug(int level, const char *file, const int line, const char *fmt, ...);

void debughex(int level, unsigned char *buf, size_t len);

#define debug(level, fmt...) _debug(level, __FILE__, __LINE__, fmt)

int chtoi(char c);

void addtime(struct custime *t);		// 时间增加一天
void deltime(struct custime *t);		// 日期上减少一天
int comdate(struct custime t1, struct custime t2); // 比较两个日期的大小，返回值：日期较大，返回正值，同一天，返回0;日期小，返回负值。
int commonth(struct custime t1, struct custime t2);
int comtime(struct custime t1, struct custime t2); // 比较两个日期的大小，返回值：日期较大，返回正值，同一天，返回0;日期小，返回负值。
int diftime(struct custime t1, struct custime t2); // 计算两个日期相差的天数
int mday(int month, int year);	// 按月份和年份，返回当月的天数
int yday(int year);			// 按年份返回当年的天数
void addmonth(struct custime *t);
void delmonth(struct custime *t);

time_t strtotime(const char *timestr, const char *format);
time_t custotime(struct custime);
struct tm custotm(struct custime, struct tm *tm);
struct custime timetocstm(const time_t);
struct custime getNow();

unsigned short crc16(unsigned short crc, unsigned char *buffer, size_t len);

int connect_nonb(int sockfd, struct sockaddr *saptr, socklen_t salen, int nsec);

int connect_server(const char *server, int port, int sec);

int base64_encode(char *dst, size_t dlen, const unsigned char *src, size_t len);

int memsearch(const unsigned char *s1, const unsigned char *s2, size_t len);

int setnonblocking(int);

double strtodouble(unsigned char *str);

char basechar(int);

int md5(const char *file, unsigned char *md);

#ifdef __cplusplus
}
#endif

string dateftime(const time_t, const char *format);
string str_difftime(time_t dif_time);

#endif /* COMMON_H_ */
